package com.itcncp.wechat.app.rpc.service.impl;

import com.itcncp.common.annotation.BaseService;
import com.itcncp.common.base.BaseServiceImpl;
import com.itcncp.wechat.app.dao.mapper.ItcsCarBrandInfoMapper;
import com.itcncp.wechat.app.dao.model.ItcsCarBrandInfo;
import com.itcncp.wechat.app.dao.model.ItcsCarBrandInfoExample;
import com.itcncp.wechat.app.rpc.api.ItcsCarBrandInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * ItcsCarBrandInfoService实现
 * Created by itcncp on 2018/1/18.
 */
@Service
@Transactional
@BaseService
public class ItcsCarBrandInfoServiceImpl extends BaseServiceImpl<ItcsCarBrandInfoMapper, ItcsCarBrandInfo, ItcsCarBrandInfoExample>
        implements ItcsCarBrandInfoService {

    private static final Logger LOGGER = LoggerFactory.getLogger( ItcsCarBrandInfoServiceImpl.class );

    @Autowired
    ItcsCarBrandInfoMapper itcsCarBrandInfoDao;

    //查询所有车辆品牌信息
    @Override
    @Cacheable(value = "itcncp-ucenter-rpc-service-ehcache", key = "'selectBrandInfo_'+#keyword")
    public List<ItcsCarBrandInfo> findBrand(String keyword){

        ItcsCarBrandInfoExample itcsCarBrandInfoExample = new ItcsCarBrandInfoExample();


        ItcsCarBrandInfoExample.Criteria  criteria =
                itcsCarBrandInfoExample.createCriteria().andParentidEqualTo( 0 );

        if(!keyword.isEmpty()){
            criteria.andNameLike( "%"+keyword+"%" );
        }

        List<ItcsCarBrandInfo> list = itcsCarBrandInfoDao.selectByExample( itcsCarBrandInfoExample );

        return  list;
    }

    //根据上级id查询车辆品牌详细
    @Override
    @Cacheable(value = "itcncp-ucenter-rpc-service-ehcache", key = "'selectBrandInfo_' + #parentid")
    public List<ItcsCarBrandInfo> findBrandCar(Integer parentid){

        ItcsCarBrandInfoExample itcsCarBrandInfoExample = new ItcsCarBrandInfoExample();
        itcsCarBrandInfoExample.createCriteria().andParentidEqualTo( parentid );

        itcsCarBrandInfoExample.setOrderByClause( "yeartype DESC" );

        List<ItcsCarBrandInfo> list = itcsCarBrandInfoDao.selectByExample( itcsCarBrandInfoExample );

        return  list;
    }

    //根据车辆品牌查询id查询品牌种类
    @Override
    @Cacheable(value = "itcncp-ucenter-rpc-service-ehcache", key = "'selectBrandTypeInfo_' + #parentid")
    public List<ItcsCarBrandInfo> findBrandType(Integer parentid){

        ItcsCarBrandInfoExample itcsTypeBrandInfoExample = new ItcsCarBrandInfoExample();
        itcsTypeBrandInfoExample.createCriteria().andParentidEqualTo( parentid );
        List<ItcsCarBrandInfo> typeids = itcsCarBrandInfoDao.selectByExample( itcsTypeBrandInfoExample );

        if(typeids!=null && typeids.size()>0){
            List<Integer> ids = new ArrayList<>(  );
            for (int i=0;i<typeids.size();i++){
                ids.add(typeids.get( i ).getId());
            }

            ItcsCarBrandInfoExample itcsCarBrandInfoExample = new ItcsCarBrandInfoExample();
            itcsCarBrandInfoExample.createCriteria().andParentidIn( ids );
            List<ItcsCarBrandInfo> list = itcsCarBrandInfoDao.selectByExample( itcsCarBrandInfoExample );

            return  list;
        }

        return  typeids;
    }

    public static void main(String[] args) {
        //保存所有品牌的车辆信息
//        SaveCarType("1");
    }

    //保存所有品牌的车辆信息
//    private void SaveCarBrand(Integer begin,Integer end) {
//        SdkHttpResult sdkHttpResult = CarBrandUtils.getBrand();
//
//        Map<String, Object> brands = JsonUtils.toMap( sdkHttpResult.getResult() );
//
//        List<ItcsCarBrandInfo> list = new ArrayList<ItcsCarBrandInfo>();
//        try {
//            list = JsonUtils.toBeanList( brands.get( "result" ).toString()  , ItcsCarBrandInfo.class );
//
//            for (int i = 0; i < list.size(); i++) {
//
//                ItcsCarBrandInfo item = new ItcsCarBrandInfo();
//
//                String josn = JsonUtils.toJson( list.get( i ) );
//
//                item = JsonUtils.toBean( josn, ItcsCarBrandInfo.class );
//
//                System.out.println("========================处理========================"+item.getName());
//
//                if(item.getId()>begin){
//
//                    if(item.getId().equals( end ) && end!=0){
//                        System.out.println("========================处理结束========================");
//                        return;
//                    }
//
//                    itcsCarBrandInfoDao.insertSelective( item );
//                    SaveCarType(item.getId().toString());
//
//                }
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        System.out.println("========================执行结束========================");
//
//    }
//
//
//    //根据品牌获取车型　保存所有的车型信息
//    private void SaveCarType(String parentid) {
//        SdkHttpResult sdkHttpResult = CarBrandUtils.getCarType( parentid );
//
//        Map<String, Object> brands = JsonUtils.toMap( sdkHttpResult.getResult() );
//
//        List<ItcsCarBrandInfo> list = new ArrayList<ItcsCarBrandInfo>();
//        try {
//
//            String result = brands.get( "result" ).toString();
//            if(result.length()<4){
//                return;
//            }
//            list = JsonUtils.toBeanList( result , ItcsCarBrandInfo.class );
//
//            if (null != list) {
//                for (int i = 0; i < list.size(); i++) {
//                    ItcsCarBrandInfo item = new ItcsCarBrandInfo();
//
//                    String josn = JsonUtils.toJson( list.get( i ) );
//                    item =
//                            JsonUtils.toBean( josn, ItcsCarBrandInfo.class );
//                    item.setParentid( new Integer( parentid ) );
//
//                    System.out.println( "＝＝＝＝＝＝＝＝保存TYPE" + item.getName() );
//                    itcsCarBrandInfoDao.insertSelective( item );
//
//                    if (null != item.getList()) {
//                        for (int j = 0; j < item.getList().size(); j++) {
//
//                            //保存车型 和该车型下所有车辆信息
//                            String parentId = item.getId().toString();
//                            String parentTypeId = item.getList().get( j ).getId().toString();
//                            SaveCar( parentId, parentTypeId );
//                        }
//                    }
//                }
//            }
//
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
//
//    //根据车型获取车　保存所有的车信息
//    private void SaveCar(String parentId, String parentTypeId) {
//        SdkHttpResult sdkHttpResult = CarBrandUtils.getCar( parentTypeId );
//
//        Map<String, Object> brands = JsonUtils.toMap( sdkHttpResult.getResult() );
//
//        String result = brands.get( "result" ).toString();
//        if(result.trim().length()<4){
//            return;
//        }
//
//        ItcsCarBrandInfo carType = new ItcsCarBrandInfo();
//
//        carType = JsonUtils.toBean( result , ItcsCarBrandInfo.class );
//        carType.setParentid( new Integer( parentId ) );
//
//        if (null != carType) {
//            System.out.println( "＝＝＝＝＝＝＝＝保存TYPECAR" + carType.getName() );
//            itcsCarBrandInfoDao.insertSelective( carType );
//
//            if (null != carType.getList()) {
//                for (int j = 0; j < carType.getList().size(); j++) {
//                    ItcsCarBrandInfo carBrandInfo = new ItcsCarBrandInfo();
//                    carBrandInfo = carType.getList().get( j );
//                    carBrandInfo.setParentid( carType.getId() );
//                    System.out.println( "＝＝＝＝＝＝＝＝保存CAR" + carBrandInfo.getName() );
//                    itcsCarBrandInfoDao.insertSelective( carBrandInfo );
//                }
//            }
//        }
//
//    }


}